import { systemDepartmentListNoPage } from "@/api/department";
import { convertListToTree } from "@/util/tree";

const state = {
  departmentList: [],
  departmentTree: {},
};

const getters = {};

const mutations = {
  set_department_list: (state, data) => {
    state.departmentList = data;
  },
  set_department_tree: (state, data) => {
    state.departmentTree = data;
  },
};

const actions = {
  getSystemDepartmentListNoPage({ commit }) {
    return new Promise((resolve, reject) => {
      systemDepartmentListNoPage()
        .then((response) => {
          commit("set_department_list", JSON.parse(JSON.stringify(response)));
          commit("set_department_tree", convertListToTree(JSON.parse(JSON.stringify(response)), "parentId"));
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
